package com.puris.service.student;

import com.puris.dao.BaseDao;
import com.puris.dao.student.*;
import com.puris.pojo.Student;

import javax.servlet.http.HttpSession;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author Sonata
 * @data 2021/11/13 15:16
 */
public class ServiceImpl implements Service {
    private LoginDao loginDao;
    private ChangePwdDao changePwdDao;
    private SelectCompetitionDao competitionDao;
    private JoinCompetitionDao joinCompetitionDao;
    private ExitCompetitionDao exitCompetitionDao;
    public ServiceImpl() {
        loginDao = new LoginDaoImpl();
        changePwdDao = new ChangePwdDaoImpl();
        competitionDao = new SelectCompetitionDaoImpl();
        joinCompetitionDao = new JoinCompetitionDaoImpl();
        exitCompetitionDao = new ExitCompetitionDaoImpl();
    }

    @Override
    public Student login(String id, String password, HttpSession session) {
        Connection connection = null;
        Student student = null;

        connection = BaseDao.getConnection();
        try {
            student = loginDao.getLoginStudent(connection, id, password, session);
            if (student != null) {
                session.setAttribute("id", student.getId());
                session.setAttribute("password", student.getPassword());
                session.setAttribute("department", student.getDepartment());
                session.setAttribute("grade", student.getGrade());
                session.setAttribute("className", student.getClassName());
                session.setAttribute("studentName", student.getStudentName());
                session.setAttribute("competitionName", student.getCompetitionName());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }

        return student;
    }

    @Override
    public boolean changePwd(HttpSession session, String oldPassword, String newPassword) {
        Connection connection = null;

        String id = (String) session.getAttribute("id");

        connection = BaseDao.getConnection();

        boolean flag = false;
        try {
            flag = changePwdDao.changePassword(connection, id, newPassword, oldPassword);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }

        return flag;
    }

    @Override
    public StringBuffer selectCompetition() {
        Connection connection = null;

        connection = BaseDao.getConnection();

        StringBuffer competition = null;
        try {
            competition = competitionDao.selectCompetition(connection);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        BaseDao.closeResource(connection, null, null);

        return competition;
    }

    @Override
    public String joinCompetition(String studentId, String competitionId) throws SQLException {
        Connection connection = null;

        connection = BaseDao.getConnection();

        String competitionName = null;

        if (joinCompetitionDao.hasCompetition(connection, competitionId) && !joinCompetitionDao.isJoin(connection, studentId)) {
            competitionName = joinCompetitionDao.joinCompetition(connection, studentId, competitionId);
        }

        BaseDao.closeResource(connection, null, null);
        return competitionName;
    }

    @Override
    public boolean exitCompetition(String id) throws SQLException {
        Connection connection = null;

        connection = BaseDao.getConnection();

        boolean flag = false;

        flag = exitCompetitionDao.exit(connection, id);
        BaseDao.closeResource(connection, null, null);

        return flag;
    }
}
